#include <string>
#include "RecognizerAPI.h"
#include "MorphologyFeatExtractor.h"
#include "RecognizerModule.h"
#include "RecognizerObjects.h"
#include "cv.h"
using namespace std;
static CMorphologyFeatExtractor MorphFeatureExtractor;
static CRecognizerModule RecModule;
static SRecObjVec DetectedObjects;

/*******************************************************************/
extern bool  InitializeRecognizer( char* fullPath, char* fname)
{
	bool retVal = true;
	string path, fileName;
	path.assign(fullPath);
	fileName.assign(fname);
	retVal = RecModule.LoadSVM(path, fileName);
	RecModule.SetFeatureExtractor(&MorphFeatureExtractor);
	return retVal; 
}
/*******************************************************************/
extern int  ProcessImage( IplImage * img )
{
	int retVal = 0;
	DetectedObjects.clear(); 
	if( NULL != img && img->imageData != NULL )
	{
		DetectedObjects = RecModule.RecognizeFrame(img);
	}
	retVal = static_cast<int>(DetectedObjects.size());
	return retVal;
}

/*******************************************************************/
extern int   GetNumberObjects( void )
{
	return static_cast<int>(DetectedObjects.size()); 
}
/*******************************************************************/
extern void  GetObjectBoundingBox( int number, int& w, int& h, int& x, int& y )
{
	if( number >= 0 && number < static_cast<int>(DetectedObjects.size()))
	{
		h = DetectedObjects[number].GetPixelHeight();
		w = DetectedObjects[number].GetPixelWidth();
		x = DetectedObjects[number].GetPixelPosX();
		y = DetectedObjects[number].GetPixelPosY(); 
	}
}
/*******************************************************************/
extern const char * GetObjectClassName(  int number/*, CString& str*/ )
{
	if( number >= 0 && number < static_cast<int>(DetectedObjects.size()))
	{
		//string temp = DetectedObjects[number].GetClassName();
		//CString cs(temp.c_str());
		//str = cs; 
		return DetectedObjects[number].GetClassName().c_str();
	}
	return NULL; 
}
/*******************************************************************/
extern double GetObjectClassLabel( int number )
{
	double retVal = 0.00;
	if( number >= 0 && number < static_cast<int>(DetectedObjects.size()))
	{
		retVal = DetectedObjects[number].GetClassType();
	}
	return retVal;  
}
/*******************************************************************/
extern void SetThreshold( int threshold )
{
	RecModule.SetThreshold(threshold);
}
/*******************************************************************/
extern int  GetThreshold( void )
{
	return RecModule.GetThreshold();
}
/*******************************************************************/
extern void SetFeatureSize( int max_area, int min_area, int minWH )
{
	RecModule.SetExtractorParams(max_area,min_area, minWH ); 
}
/*******************************************************************/
extern void LoadCalibMatrix( char * fname, int size, float* matrix )
{
	if(size != 9)
		return;
	CvMemStorage* memStorage;
	memStorage = cvCreateMemStorage();
	CvFileStorage* fs = cvOpenFileStorage( fname, memStorage,CV_STORAGE_READ);
	CvMat* calib  = (CvMat*) cvReadByName( fs,0,"intrinsic_matrix");
	int idx = 0; 
	for( int i=0; i<3; i++ )
	{
		for( int j=0; j<3; j++ )
		{
			CvScalar temp = cvGet2D(calib, i, j );
			matrix[idx] = static_cast<float>(temp.val[0]);
			idx++;
		}
	}
	cvReleaseFileStorage( &fs );
	cvReleaseMemStorage( &memStorage ); 
}
/*******************************************************************/
